package main

import (
	"context"
	"encoding/json"
	"git.900sui.cn/kc/kcgin"
	"git.900sui.cn/kc/kcgin/logs"
	"git.900sui.cn/kc/rabbitmq"
	"git.900sui.cn/kc/rpcinterface/client/public"
	public2 "git.900sui.cn/kc/rpcinterface/interface/public"
	"git.900sui.cn/kc/rpcinterface/interface/task"
	"github.com/streadway/amqp"
)

//发送邮件
func main() {
	logs.Info("Environment Variable:MSF_ENV:",kcgin.KcConfig.RunMode)
	rpcMail := new(public.Email).Init()
	defer rpcMail.Close()

	err := rabbitmq.Rabbitmq.Consume(kcgin.AppConfig.String("queue.sendMail"), func(msg amqp.Delivery)  {
		var mailData task.SendMailParams
		err := json.Unmarshal( msg.Body, &mailData )
		if err != nil{
			return
		}

		ctx := context.Background()
		var reply = false
		if mailData.Type == task.SEND_MAIL_TYPE_captcha{
			//发送验证码
			args := &public2.SendMailCaptchaParams{
				Email:mailData.Email,
				CheckSendNum:mailData.CheckSendNum,
			}
			err := rpcMail.SendCaptcha( ctx, args, &reply )
			if err != nil{
				logs.Info(err)
				return
			}
		}
		if mailData.Type == task.SEND_MAIL_TYPE_plain{
			//发送普通短信
			args := &public2.SendMailParams{
				Email:mailData.Email,
				MsgStr:mailData.MsgStr,
				Subject:mailData.Subject,
				FromMail:mailData.FromMail,
				FromName:mailData.FromName,
			}
			err := rpcMail.SendMsg( ctx, args, &reply )
			if err != nil{
				return
			}
		}

		return
	})
	if err != nil {
		panic(err)
	}
}
